import { useEffect } from 'react';
import { useAppStore } from '@/stores/appStore';
import type { ZKProofData } from '@/types';

export function useDataLoader() {
  const { setZKProofData, setError } = useAppStore();
  
  useEffect(() => {
    const loadZKProofData = async () => {
      try {
        // Load proof data
        const proofResponse = await fetch('/src/data/proof.json');
        const proofData = await proofResponse.json();
        
        // Load public signals
        const publicResponse = await fetch('/src/data/public.json');
        const publicData = await publicResponse.json();
        
        // Load input data
        const inputResponse = await fetch('/src/data/input.json');
        const inputData = await inputResponse.json();
        
        // Combine into ZKProofData format
        const zkProofData: ZKProofData = {
          input: inputData,
          proof: proofData,
          public: publicData
        };
        
        setZKProofData(zkProofData);
        console.log('✅ ZK Proof data loaded successfully');
        console.log('📊 Data summary:', {
          inputPreImage: inputData.preImage,
          proofSize: JSON.stringify(proofData).length,
          publicSignals: publicData.length
        });
        
      } catch (error) {
        console.error('❌ Failed to load ZK proof data:', error);
        setError('Failed to load demonstration data');
      }
    };
    
    loadZKProofData();
  }, [setZKProofData, setError]);
  
  // Preload critical assets
  useEffect(() => {
    const preloadAssets = async () => {
      try {
        // Preload any critical animations or images
        const criticalAssets: string[] = [
          // Add any critical asset URLs here
        ];
        
        await Promise.all(
          criticalAssets.map(async (url) => {
            try {
              await fetch(url);
            } catch (error) {
              console.warn(`Failed to preload asset: ${url}`, error);
            }
          })
        );
        
        console.log('✅ Critical assets preloaded');
      } catch (error) {
        console.warn('⚠️ Some assets failed to preload:', error);
      }
    };
    
    preloadAssets();
  }, []);
}